import { createStore, applyMiddleware, combineReducers } from './redux';
import thunk from './middlewares/redux-thunk';
import thunkArray from './middlewares/redux-thunk-array';
import promiseMiddleware from './middlewares/promiseMiddleware';
import reducers from './reducer';
import { INCREMENT, DECREMENT, CHANGE_AGE } from './actionType';
import timeMiddleware from './middlewares/timeMiddleware';
import loggerMiddleware from './middlewares/loggerMiddleware';

export default function configureStore() {
  const store = createStore(
    combineReducers(reducers),
    applyMiddleware(thunk, thunkArray)
    // applyMiddleware(promiseMiddleware)
  );

  // 中间件嵌套手动调用
  // const next = store.dispatch;
  // const logger = loggerMiddleware(store);
  // const time = timeMiddleware(store);
  // const thunkMidd = thunk(store);
  // store.dispatch = logger(time(thunkMidd(next)));

  // const init = store.getState();
  // console.log(`小月开始是 ${init.counterReducer.age} 岁`);

  // function listener() {
  //   const current = store.getState();
  //   console.log('current: ', current.counterReducer.age);
  //   console.log(`小月现在是 ${current.counterReducer.age} 岁`);
  // }

  // // 订阅，每次state修改，都会执行 listener
  // store.subscribe(listener);
  // // 提交状态变更(dispatch)
  // store.dispatch({ type: INCREMENT });
  // store.dispatch({ type: INCREMENT });
  // store.dispatch({ type: INCREMENT });
  // store.dispatch({ type: DECREMENT });
  // store.dispatch({ type: DECREMENT });

  return store;
}
